//
//  LoginMethodView.swift
//  ezyform
//
//  Created by Moennig Q on 19/7/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class LoginMethodView: MeModelVCBaseView {
    var settingClosure: ((LoginMethodModel, Bool)->Void)?
    var toGestureSettingClosure: (()->Void)?
    var list: [LoginMethodModel]! {
        didSet {
            if let _ = self.list {
                self.tableViewHeightConstrains?.update(offset: cellHeight * CGFloat(self.list.count))
            }
            
        }
    }
    lazy var settingTipLabel: BaseLabel = {
        let label = BaseLabel()
        label.text = "You can change it later in 'Me' page"
        label.setFont(R.font.sfProTextRegular(size: 14))
        label.textColor = UIColor(hexStr: "#9B9B9B")
        label.textAlignment = NSTextAlignment.center
        label.numberOfLines = 0
        return label
    }()
    let tableView = UITableView()
    var cellHeight: CGFloat = 56
    var tableViewHeightConstrains: Constraint?
    //MARK: 是否是引导图;如果是引导图则显示全屏；否则显示为定制的model样式（me里边的样式）
//    init(_ isGuardFlag: Bool) {
//        <#statements#>
//    }
    override func setUpviews() {
        self.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(self.topBackView.snp.bottom)
            make.left.right.equalToSuperview()
            self.tableViewHeightConstrains = make.height.equalTo(0).constraint
        }
        let bottomView = UIView()
        bottomView.backgroundColor = UIColor(hexStr: "#F5F5F5")
        self.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.top.equalTo(self.tableView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        tableView.register(LoginMethodTableViewCell.self, forCellReuseIdentifier: LoginMethodTableViewCell.reuseCellIdentifier)
        tableView.register(GestureSettingTableViewCell.self, forCellReuseIdentifier: GestureSettingTableViewCell.reuseCellIdentifier)
        tableView.separatorStyle = .singleLine
        tableView.separatorColor = UIColor(hexStr: Constant.SeparatorColorHexStr)
        tableView.separatorInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.isScrollEnabled = false//不可滚动
        //tip label
        let tipLabel = ContentLabel(R.font.interRegular(size: 13),hexStr: "#292D3266")
        tipLabel.text = R.string.localizable.loginMethodSettingValidtip()
        tipLabel.textAlignment = NSTextAlignment.center
        tipLabel.numberOfLines = 0
        self.addSubview(tipLabel)
        tipLabel.snp.makeConstraints { make in
            make.top.equalTo(tableView.snp.bottom).offset(30)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.greaterThanOrEqualTo(0)
        }
        //setting tip label
        self.settingTipLabel.isHidden = false
        self.addSubview(settingTipLabel)
        settingTipLabel.snp.makeConstraints { make in
            make.top.equalTo(tipLabel.snp.bottom).offset(8)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.greaterThanOrEqualTo(0)
        }
    }
}
extension LoginMethodView: UITableViewDataSource, UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let _ = self.list else {
            return 0
        }
        return self.list.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.cellHeight
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if self.list[indexPath.row].operType == LoginMethodOperation.gestureSetting {
            var cell: GestureSettingTableViewCell? = tableView.dequeueReusableCell(withIdentifier: GestureSettingTableViewCell.reuseCellIdentifier, for: indexPath) as? GestureSettingTableViewCell
            if cell == nil {
                cell = GestureSettingTableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: GestureSettingTableViewCell.reuseCellIdentifier)
            }
            return cell!
        }else{
            var cell: LoginMethodTableViewCell? = tableView.dequeueReusableCell(withIdentifier: LoginMethodTableViewCell.reuseCellIdentifier, for: indexPath) as? LoginMethodTableViewCell
            if cell == nil {
                cell = LoginMethodTableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: LoginMethodTableViewCell.reuseCellIdentifier)
            }
            cell?.setData(list[indexPath.row])
            if list[indexPath.row].operType == LoginMethodOperation.gesture{
                cell?.switchBtnClosure = { [weak self] (isOn) in
                    guard let strongSelf = self else { return  }
                    strongSelf.settingClosure?(strongSelf.list[indexPath.row], isOn)
                }
            }else{
                cell?.switchBtnClosure = { [weak self] (isOn) in
                    guard let strongSelf = self else { return  }
                    strongSelf.settingClosure?(strongSelf.list[indexPath.row], isOn)
                }
            }
            return cell!
        }
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.list[indexPath.row].operType == LoginMethodOperation.gestureSetting {
            self.toGestureSettingClosure?()
        }
    }
}
